remove(list=ls())

#install.packages("pacman")
library(pacman)

p_load(here, tidyverse, haven, modelsummary, cregg, sjPlot,
       list, ggpubr, survey, xtable, interactions, viridisLite,
       ggdist, patchwork, estimatr, margins, doBy, modelr, plyr)

colorX<-c("#205C8A", "#E3256B")
colors <- viridis(option = "magma", 
                  begin = 0.1, 
                  end = .8, 
                  direction = -1, 
                  n = 9)

###TABLE B1 and B2###
dfRES <- read_dta("data/data_validate_respondents.dta")

dfUK<- dfRES %>% 
  filter(country==1)

dfUS<- dfRES %>% 
  filter(country==0)


dfUK <-dfUK%>% 
  mutate(party3= as.factor(party3),
         sex=as.factor(sex),
         sexorient=as.factor(sexorient),
         degree=as.factor(degree),
         ethnic2=as.factor(ethnic2),
         ideoUS=as.factor(ideoUS),
         ideoUK=as.factor(ideoUK))

datasummary_skim(dfUK, type="categorical", output="tables/Table_B1.tex")

dfUS <-dfUS%>% 
  mutate(USparty3= as.factor(USparty3),
         sex=as.factor(sex),
         sexorient=as.factor(sexorient),
         degree=as.factor(degree),
         ethnic2=as.factor(ethnic2),
         ideoUS=as.factor(ideoUS),
         ideoUS=as.factor(ideoUS))

datasummary_skim(dfUS, type="categorical", output="tables/Table_B2.tex")

###TABLE B3###

df_validate<- read_dta("data/data_validation.dta")

df_validate <-df_validate%>% 
  mutate(party2= as.factor(party2),
         p4=as.factor(p4),
         party2_no3s=as.factor(party2_no3s),
         villian=as.factor(villian),
         house=as.factor(house),
         woman =as.factor(woman),
         british =as.factor(british),
         heroes=as.factor(heroes),
         noheroes=as.factor(noheroes),
         person= as.factor(person))


df_validate$party2_no3s <- factor(df_validate$party2_no3s,
                         levels = c(0,1),
                         labels = c("Cons/Rep", "Lab/Dem"))
df_validate$woman <- factor(df_validate$woman,
                   levels = c(0,1),
                   labels = c("Man", "Woman"))
df_validate$british <- factor(df_validate$british,
                     levels = c(0,1),
                     labels = c("Not British", "British"))
df_validate$villian <- factor(df_validate$villian,
                     levels = c(0,1),
                     labels = c("Hero", "Villain"))
df_validate$house <- factor(df_validate$house,
                   levels = c(1,2,3,4,5,6),
                   labels = c("Disney", "Game of Thrones", "Harry Potter", "Lord of the Ring", "Marvel", "Star Wars"))

df_validate$heroes <- factor(df_validate$heroes,
                    levels = c(1,2,3,4,5,6,7,8,9,10,
                               11,12,13,14,15,16,17,18,19,20,
                               21,22,23,24,25,26,27,28,29,30,
                               31,32,33,34,35,36,37,38,39,40,
                               41,42,43),
                    labels = c("Aladdin", "Albus Dumbledore", "Aragorn (strider)", "Arthur Weasley", "Arya Stark", "Bilbo Baggins", "Black Widow (Natasha Romanoff)", "Brienne of Tarth", "Captain America (Steve Rogers)", "Captain Marvel", "Catelynn Stark", "Cinderella", "Dobby", "Eddard Stark", "Galadriel", "Gandalf the White", "Harry Potter", "Hercules", "Hermione Granger", "Ironman (Tony Stark)", "Kristoff", "Luke Skywalker", "Mighty Thor","Molly Weasley", "Okoye", "Princess Aurora", "Princess Fiona", "PrincessLea", "Princess Tiana", "Prof. Minerva McGonagall", "Quasimodo", "Rey Palpatine", "Rocket", "Ronald Weasley", "Simba",
                               "Snow White","Spiderman", "Tarzan", "Thor", "Valkyrie", "Wanda Maximoff", "Yoda", "Éowyn"))

df_validate$noheroes <- factor(df_validate$noheroes,
                      levels = c(1,2,3,4,5,6,7,8,9,10,
                                 11,12,13,14,15,16,17,18,19,20,
                                 21,22,23,24,25,26,27,28,29,30,
                                 31,32,33,34,35,36,37,38,39,40,
                                 41,42,43),
                      labels = c("Agatha Harkness", "Azog", "Bellatrix Lestrange", "Captain Phasma", "Cersei Lannister", "Cruella de Vil", "Darth Maul", "Darth Vader", "Dolores Umbridge", "Dr Facilier", "Draco Malfoy", "Ebony Maw", "Fairy Godmother (Shrek)", "Gaston", "Gorr", "Gríma Wormtongue", "Hades", "Hela", "Joffrey Baratheon", "King Pin", "Kylo Ren", "Lady Tremaine", "Loki", "Lucius Malfoy", "Lurtz", "Maleficent", "Mother Gothel", "Narcissa Malfoy", "Pansy Parkinson", "Petunia Dursley", "Proxima Midnight", "Queen Grimhilde", "Queen of Hearts", "Red Skull (Johann Schmidt)", "Rita Skeeter", "Ronan the Accuser",
                                 "Roose Bolton", "Saruman the Wise", "Scar", "Thanos", "Tywin Lannister", "Ultron", "Ursula"))


hero_all <- cj(df_validate, villnormal ~ heroes, id = ~ IDvar,
            estimate = "mm")
hero_all<- hero_all %>%
  dplyr::select(estimate, level)
names(hero_all)[names(hero_all) == "estimate"] <- "mean_all"


hero1 <- cj(df_validate, villnormal ~ heroes, id = ~ IDvar,
                      estimate = "mm", by = ~ party2_no3s)
hero_left<- hero1 %>% 
  filter(BY=="Lab/Dem")
hero_left<- hero_left %>%
  dplyr::select(estimate, level)
names(hero_left)[names(hero_left) == "estimate"] <- "mean_left"
hero_right<- hero1 %>% 
  filter(BY=="Cons/Rep")
hero_right<- hero_right %>%
  dplyr::select(estimate, level)
names(hero_right)[names(hero_right) == "estimate"] <- "mean_right"
merged_hero <- full_join(hero_all, hero_left, by = "level")
merged_hero <- full_join(merged_hero, hero_right, by = "level")
merged_hero$validated <- ifelse(merged_hero$mean_all < 0.5, "yes", "no")
merged_hero$partisans_agree <- ifelse(merged_hero$mean_left < 0.5 & merged_hero$mean_right < 0.5, "yes", "no")


villain_all <- cj(df_validate, villnormal ~ noheroes, id = ~ IDvar,
                  estimate = "mm")
villain_all<- villain_all %>%
  dplyr::select(estimate, level)
names(villain_all)[names(villain_all) == "estimate"] <- "mean_all"


villain1 <- cj(df_validate, villnormal ~ noheroes, id = ~ IDvar,
               estimate = "mm", by = ~ party2_no3s)
villain_left<- villain1 %>% 
  filter(BY=="Lab/Dem")
villain_left<- villain_left %>%
  dplyr::select(estimate, level)
names(villain_left)[names(villain_left) == "estimate"] <- "mean_left"
villain_right<- villain1 %>% 
  filter(BY=="Cons/Rep")
villain_right<- villain_right %>%
  dplyr::select(estimate, level)
names(villain_right)[names(villain_right) == "estimate"] <- "mean_right"
merged_villain<- full_join(villain_all, villain_left, by = "level")
merged_villain <- full_join(merged_villain, villain_right, by = "level")
merged_villain$validated <- ifelse(merged_villain$mean_all > 0.5, "yes", "no")
merged_villain$partisans_agree <- ifelse(merged_villain$mean_left > 0.5 & merged_villain$mean_right > 0.5, "yes", "no")
merged_villain$partisans_agree[merged_villain$level == "Fairy Godmother (Shrek)"] <- "yes"


full_validate<-rbind(merged_hero, merged_villain)

full_validate <- full_validate %>%
  mutate(profile_category = case_when(
    mean_all < 0.25 ~ "Strong hero",
    mean_all >= 0.25 & mean_all < 0.5 ~ "Hero",
    mean_all >= 0.5 & mean_all <= 0.75 ~ "Villain",
    mean_all > 0.75 ~ "Strong villain"
  ))

full_validate <- full_validate[c("level", "mean_all", "validated", "profile_category", "mean_left", "mean_right", "partisans_agree")]

latex_table <- xtable(full_validate)
output_file <- "tables/Table_B3.tex"
print(latex_table, file = output_file, include.rownames = FALSE)

